package com.xindaoapp.happypet.fragments;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import com.xindao.photonics.fragments.manager.BaseFragment;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.User;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.IRequest;
import com.xindaoapp.happypet.utils.LoginManager;
import com.xindaoapp.happypet.utils.MoccaPreferences;
import com.xindaoapp.happypet.utils.ProgressHUD;

/**
 * 创建用户
 * 
 * @author chenghao
 * 
 */
public class CreateUserFragment extends BaseFragment implements OnClickListener {
    private EditText vNickName;
    private View vCreateUser;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.layout_create_user, container, false);
    }

    @Override
    protected void onFragmentResult(Bundle bundle) {
        super.onFragmentResult(bundle);

    }

    @Override
    protected void initEvents(View view) {
        super.initEvents(view);
        vNickName.addTextChangedListener(mTextWatcher);
        view.findViewById(R.id.bind_account).setOnClickListener(this);
        view.findViewById(R.id.create_account).setOnClickListener(this);
    }

    @Override
    protected void initViews(View view) {
        super.initViews(view);
        vNickName = (EditText) view.findViewById(R.id.username);

        vCreateUser = view.findViewById(R.id.create_account);
        if (getArguments().getInt(Constants.KEY_LOGIN_TYPE) == Constants.LOGINMODE.QQ.ordinal()
                && !TextUtils.isEmpty(MoccaPreferences.QQ_NICKNAME.get())) {
            vNickName.setText(MoccaPreferences.QQ_NICKNAME.get().replaceAll(" ", ""));
            vCreateUser.setEnabled(true);
        } else if (getArguments().getInt(Constants.KEY_LOGIN_TYPE) == Constants.LOGINMODE.SINA
                .ordinal() && !TextUtils.isEmpty(MoccaPreferences.SINA_USER_NAME.get())) {
            vNickName.setText(MoccaPreferences.SINA_USER_NAME.get().replaceAll(" ", ""));
            vCreateUser.setEnabled(true);
        } else if (getArguments().getInt(Constants.KEY_LOGIN_TYPE) == Constants.LOGINMODE.WeiXin
                .ordinal() && !TextUtils.isEmpty(MoccaPreferences.WeiXin_USER_NAME.get())) {
            vNickName.setText(MoccaPreferences.WeiXin_USER_NAME.get().replaceAll(" ", ""));
            vCreateUser.setEnabled(true);
        }
        vNickName.requestFocus();
        showKeyBoard();
    }

    private final TextWatcher mTextWatcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (TextUtils.isEmpty(vNickName.getText().toString())) {
                vCreateUser.setEnabled(false);
            } else {
                vCreateUser.setEnabled(true);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

        @Override
        public void afterTextChanged(Editable s) {}
    };

    @Override
    protected String getTitleName() {
        return "创建用户";
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.create_account:
                if (checkInput()) {
                    if (getArguments().getInt(Constants.KEY_LOGIN_TYPE) == Constants.LOGINMODE.QQ
                            .ordinal()) {
                        final ProgressHUD ProgressHUB =
                                ProgressHUD.show(mActivity, "正在登陆...", true, true, null);
                        final Bundle bundle = new Bundle();
                        bundle.putString("userName", vNickName.getText().toString());
                        new LoginManager(mActivity).quickLoginByQQ(vNickName.getText().toString(),
                                true, new IRequest<User>() {
                                    @Override
                                    public void request(User t) {
                                        if (ProgressHUB != null) {
                                            ProgressHUB.dismiss();
                                        }
                                        if (t != null) {// 说明绑定成功
                                            bundle.putSerializable("user", t);
                                            startFragmentAndClearStack(new RequestCodeFragment(),
                                                    bundle);
                                        }
                                    }
                                });
                    } else if (getArguments().getInt(Constants.KEY_LOGIN_TYPE) == Constants.LOGINMODE.SINA
                            .ordinal()) {
                        final ProgressHUD ProgressHUB =
                                ProgressHUD.show(mActivity, "正在登陆...", true, true, null);
                        final Bundle bundle = new Bundle();
                        bundle.putString("userName", vNickName.getText().toString());
                        new LoginManager(mActivity).quickLoginBySina(
                                vNickName.getText().toString(), true, new IRequest<User>() {

                                    @Override
                                    public void request(User t) {
                                        if (ProgressHUB != null) {
                                            ProgressHUB.dismiss();
                                        }
                                        if (t != null) {// 说明绑定成功
                                            bundle.putSerializable("user", t);
                                            startFragment(new RequestCodeFragment(), bundle);
                                        }
                                    }

                                });
                    } else if (getArguments().getInt(Constants.KEY_LOGIN_TYPE) == Constants.LOGINMODE.WeiXin
                            .ordinal()) {
                        final ProgressHUD ProgressHUB =
                                ProgressHUD.show(mActivity, "正在登陆...", true, true, null);
                        final Bundle bundle = new Bundle();
                        bundle.putString("userName", vNickName.getText().toString());
                        new LoginManager(mActivity).quickLoginByWeiXin(vNickName.getText()
                                .toString(), true, new IRequest<User>() {

                            @Override
                            public void request(User t) {
                                if (ProgressHUB != null) {
                                    ProgressHUB.dismiss();
                                }
                                if (t != null) {// 说明绑定成功
                                    bundle.putSerializable("user", t);
                                    startFragment(new RequestCodeFragment(), bundle);
                                }
                            }

                        });
                    }
                }
                break;
            case R.id.bind_account:
                Bundle bundle = new Bundle();
                bundle.putInt(Constants.KEY_LOGIN_TYPE,
                        getArguments().getInt(Constants.KEY_LOGIN_TYPE));
                startFragment(new BindUserFragment(), bundle);
                break;

            default:
                break;
        }
    }

    private boolean checkInput() {
        if (TextUtils.isEmpty(vNickName.getText().toString())) {
            Toast.makeText(mActivity, "昵称不能为空！", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}
